import { reactive, watch, ref } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getFactorList } from '@/apis/system/config/factorList'

export const useFactorList = () => {
  const searchForm = reactive({
    dataUnit: undefined,
    type: undefined,
    sourceType: undefined,
    version: undefined,
    factorName: undefined
  })
  const tableRef = ref()
  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getFactorList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      dataUnit: searchForm?.dataUnit,
      type: searchForm?.type,
      sourceType: searchForm?.sourceType,
      version: searchForm?.version,
      factorName: searchForm.factorName,
      status: 'ENABLE'
    }
    params = removeUndefinedParams(params)
    return params
  }
  watch(searchForm, () => {
    tableRef.value?.refresh(true)
  })
  return {
    searchForm,
    loadData,
    tableRef
  }
}
